package cn.sinonet.mb.processhandler;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import jt.classic.system.ISystem;
import jt.classic.system.ISystemException;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.init.AbstractInitializtion;
import jt.classic.system.utils.BaseworkSelectUtil;

import org.limp.basework.SimpleBean4Table;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

/**
 * 过程控制器初始化内容
 * 
 * @author mido
 * 
 */
public class MPHInitializtion extends AbstractInitializtion {
	public void load() throws ISystemException {
		// TODO Auto-generated method stub
		Connection conn = null;
		boolean validConnHanlde = false;

		try {
			SystemDataSource ds = SystemDataSource.getInstance();
			conn = ds.getConnection();

			BaseworkSelectUtil selectUtil = new BaseworkSelectUtil(conn);

			List list = selectUtil.selectMultiData("SNMB_PRSHANDLER", null,
					null);

			if (list != null && !list.isEmpty()) {
				BaseworkUtil util = new BaseworkUtil();

				for (int i = 0; i < list.size(); i++) {
					Map info = (Map) list.get(i);

					String valid = (String) info.get("PHVALID");
					String className = (String) info.get("PHCLASS");

					if (MPHCenter.validHandlerClass(className)
							&& valid.equals("-1")) {
						validConnHanlde = true;

						SimpleBean4Table sb = new SimpleBean4TableImpl(
								"SNMB_PRSHANDLER");
						sb.setColumnValue("PKID", info.get("PKID"));

						sb.setColumnValue("PHVALID", "0");
						util.manualUpdate1(sb, conn);
					} else if (!MPHCenter.validHandlerClass(className)
							&& valid.equals("0")) {
						validConnHanlde = true;

						SimpleBean4Table sb = new SimpleBean4TableImpl(
								"SNMB_PRSHANDLER");
						sb.setColumnValue("PKID", info.get("PKID"));
						sb.setColumnValue("PHVALID", "-1");
						util.manualUpdate1(sb, conn);
					}

				}
			}

			if (validConnHanlde) {
				conn.commit();
			}
		} catch (Exception ex) {
			if (validConnHanlde) {
				try {
					conn.rollback();
				} catch (Exception ex1) {
					ISystem.catchException(ex1);
				}
			}
			throw new ISystemException("MPHInitializtion isa bad-boy.", ex);
		} finally {
			SystemDataSource.closeConnection(conn);
		}

		try {
			SystemDataSource ds = SystemDataSource.getInstance();
			conn = ds.getConnection();

			BaseworkSelectUtil selectUtil = new BaseworkSelectUtil(conn);

			List list = selectUtil.selectMultiData("SNMB_PRSHANDLER", null,
					null);

			if (list != null && !list.isEmpty()) {
				MPHCenter center = MPHCenter.getInstance();

				for (int i = 0; i < list.size(); i++) {
					Map info = (Map) list.get(i);

					center.setHandler(info.get("PKID"), info);
				}
			}
		} catch (Exception ex) {
			throw new ISystemException("MPHInitializtion isa bad-boy.", ex);
		} finally {
			SystemDataSource.closeConnection(conn);
		}

		System.out
				.println("boxer message: message-process-handler all load success.");
	}
}
